package promote.brozen.reactor;

import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;

/**
 * @author Brozen
 * @date 2020/6/27 11:27 AM
 * @email brozen@qq.com
 */
public class FluxConcat {

    public static void main(String[] args) {
        concat1();
    }

    private static void concat0() {
        Flux.just("Start")
                .concatWith(createPublisher())
                .subscribe(System.out::println, Throwable::printStackTrace, () -> {
                    System.out.println("complete");
                });
    }

    private static void concat1() {
        Flux.concat(Flux.just("Start"), createPublisher()).subscribe(System.out::println, Throwable::printStackTrace, () -> {
            System.out.println("complete");
        });
    }


    private static Publisher<String> createPublisher() {
        return subscriber -> {
            int i = 0;
            while (true) {
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (i > 5) {
                    subscriber.onComplete();
                    break;
                }

                subscriber.onNext("" + i++);
            }
        };
    }

}
